﻿--[[
   @brief    ui logic for sub account list
   @author   sogoodchen
   @version  1.0
   @date     2016-05-21
--]]

sub_account_view =
{
    game_id = 0;
    id_to_uin = {};

    OPT_BIND = 1;
    OPT_UNBIND = 2;
    OPT_EDIT = 3;
    OPT_LAUNCH = 4;

    init = function()
        ierd_tgp.tpf_ui.Log("sub_account_view init.");
        ierd_tgp.tpf_ui.Require('xiaohao\\sub_account_mgr.lua');
        game_menue_control.register_main_frame_pos_change_evt();
    end;

    show = function(game_id)
      local main_dlg_id = ui_utility.get_ctrl_id_by_cid(CID_MAINFRAME_Dialog_MainFrame);
      if main_dlg_id == Tenio.INVALID_CONTROLID then
        return;
      end;

      local pdlg = ui_utility.get_ctrl_ptr_by_cid(CID_SUB_ACCOUNT_LIST_DIG);
      if pdlg == nil then
        layout_mgr.restore_sub_layout(main_dlg_id, "sub_account_list.tpf");
        pdlg = ui_utility.get_ctrl_ptr_by_cid(CID_SUB_ACCOUNT_LIST_DIG);
      end;
      if pdlg == nil then
        return;
      end;
      
      sub_account_view.game_id = game_id;
      sub_account_view.refresh_sub_account_list();

      pdlg:Show(TRUE);
      sub_account_view.on_main_frame_pos_change();
    end;

    -- 响应鼠标进入小号项的消息
    on_account_item_hover = function(uilib, id)
      local item_ctn_id = ui_utility.get_ctrl_parent_id(id);
      ui_utility.setproperty_by_path(item_ctn_id, "OpCtn", "Visible", "true");
      local icon = "ONEIMAGE:mult_account\\sub_account_list.png?74,131,122,179;Center;null;";
      ui_utility.setproperty_by_path(item_ctn_id, "ImgBtn", "Background", icon);
    end;

    -- 响应鼠标离开小号项的消息
    on_account_item_leave = function(uilib, id)
      local item_ctn_id = ui_utility.get_ctrl_parent_id(id);
      ui_utility.setproperty_by_path(item_ctn_id, "OpCtn", "Visible", "false");
      local icon = "ONEIMAGE:mult_account\\sub_account_list.png?14,131,62,179;Center;0;";
      ui_utility.setproperty_by_path(item_ctn_id, "ImgBtn", "Background", icon);
    end;

    -- 响应Dialog控件关闭消息
    on_win_close = function(uilib, id)
      sub_account_view.do_close();
      return true;
    end;

    -- 响应关闭按钮被点击的消息
    on_close_btn_clk = function(uilib, id)
      sub_account_view.do_close();
    end;

    -- 响应添加小号按钮被点击的消息
    on_add_account_btn_clk = function(uilib, id)
      local ma_mgr =
          base_utility.get_component_adapter("IMutiAccountMgr",
              "ierd_tgp.muti_account_mgr.GetMutiAccountMgrModule();");
      local subacclist = ierd_tgp.muti_account_mgr.AccountList:new_local();
      ma_mgr:get_account_list(sub_account_view.game_id, subacclist);

      if subacclist.cnt >= 3 then
        local param = {
          first_line = LoadStr(ID_TOMUCH_SUB_ACCOUNT_TIP_FIRST),
          second_line = LoadStr(ID_TOMUCH_SUB_ACCOUNT_TIP_SECOND),
          icon = "ONEIMAGE:bottom_bar\\tips_icon\\red_icon.png;Stretch;0;",
        }
        event_center.send_event(sub_account_login_event.SUB_ACCOUNT_SHOW_TIPS, 
            base_utility.table_to_json(param));
      else
        local info = {game_id = sub_account_view.game_id};
        event_center.send_event(sub_account_login_event.SUB_ACCOUNT_ADD_ACCOUNT, 
          base_utility.table_to_json(info));
      end;
    end;

    -- 响应小号登录成功消息
    on_sub_account_login_success = function(json_str)
      local tbl = base_utility.json_to_table(json_str);
      if tbl and tbl.uin and tbl.uin ~= 0 then
        local bind_op = true;
        for id, uin in pairs(sub_account_view.id_to_uin) do
          if tostring(uin) == tostring(tbl.uin) then
            bind_op = false;
            break;
          end;
        end;
        if bind_op then
          sub_account_view.report_account_operation(sub_account_view.OPT_BIND, tbl.uin);
        end;
      end;

      local pdlg = ui_utility.get_ctrl_ptr_by_cid(CID_SUB_ACCOUNT_LIST_DIG);
      if pdlg ~= nil and pdlg:IsVisible() == TRUE then
        sub_account_view.refresh_sub_account_list();
      end;
    end;

    -- 响应删除小号按钮被点击的消息
    on_del_account_btn_clk = function(uilib, id)
      local uin = sub_account_view.get_uin_by_id(id);
      if uin == 0 or uin == nil then
        return;
      end;

      sub_account_mgr.del_sub_account(uin);
    end;

    -- 响应小号删除结束消息
    on_sub_account_del_ret = function(json_str)
      local pdlg = ui_utility.get_ctrl_ptr_by_cid(CID_SUB_ACCOUNT_LIST_DIG);
      if pdlg ~= nil and pdlg:IsVisible() == TRUE then
        sub_account_view.refresh_sub_account_list();
      end;

      local info = base_utility.json_to_table(json_str);
      local uin = 0;
      if info and info.uin then
        uin = info.uin;
      end;
      sub_account_view.report_account_operation(sub_account_view.OPT_UNBIND, uin);

      if info.result == true then
        local ma_mgr =
            base_utility.get_component_adapter("IMutiAccountMgr",
                "ierd_tgp.muti_account_mgr.GetMutiAccountMgrModule();");
        ma_mgr:del_account_info(tonumber(uin));
      end;
    end;

    -- 响应开始按钮被点击的消息
    on_launch_btn_clk = function(uilib, id)
      local uin = sub_account_view.get_uin_by_id(id);
      if uin == 0 or uin == nil then
        return;
      end;

      sub_account_mgr.login_sub_account(sub_account_view.game_id, uin,
                ierd_tgp.game_library.LAUNCH_OP_FROM_SUB_ACCOUNT_LIST);

      local parent_id = ui_utility.get_ctrl_parent_id(id);
      ui_utility.setproperty_by_id(parent_id, "Visible", "false");

      sub_account_view.report_account_operation(sub_account_view.OPT_LAUNCH, uin);
    end;

    -- 响应主窗口位置移动消息的消息
    on_main_frame_pos_change = function()
      local pdlg = ui_utility.get_ctrl_ptr_by_cid(CID_SUB_ACCOUNT_LIST_DIG);
      if pdlg == nil then
        return;
      end;
      local pmain_dlg = ui_utility.get_ctrl_ptr_by_cid(CID_MAINFRAME_Dialog_MainFrame);
      if pmain_dlg == nil then
        return;
      end;

      if pdlg:IsVisible() == FALSE  or pmain_dlg:IsVisible() == FALSE then
        return;
      end;

      local main_dlg_rc = pmain_dlg:GetRectEx(FALSE,TRUE);
      local main_dlg_width = main_dlg_rc.right - main_dlg_rc.left;
      local main_dlg_height = main_dlg_rc.bottom - main_dlg_rc.top;
      if main_dlg_width == 0 or main_dlg_height == 0 then
        return;
      end;

      local dlg_rc = pdlg:GetRectEx(FALSE,TRUE);
      local dlg_width = dlg_rc.right - dlg_rc.left;
      local dlg_height = dlg_rc.bottom - dlg_rc.top;
      dlg_rc.left = (main_dlg_width - dlg_width) / 2;
      dlg_rc.right = dlg_rc.left + dlg_width;
      dlg_rc.bottom = main_dlg_height - 68;
      dlg_rc.top = dlg_rc.bottom - dlg_height;

      pdlg:MoveEx(dlg_rc,TRUE);
      pdlg:BringToTop();
    end;

    -- 响应小号昵称鼠标左键弹起消息
    on_desc_edt_lbtn_up = function(uilib, id)
      local pstc = ui_utility.get_ctrl_ptr_by_id(id)
      if pstc == nil then
        return;
      end;

      local cui_nickname = pstc:GetText();
      local u8_nickname = CUIStrtoU8(cui_nickname);

      local parent_id = ui_utility.get_ctrl_parent_id(id);
      ui_utility.setproperty_by_path(parent_id, "DescCtn\\DescEdt", "Caption", u8_nickname);
      ui_utility.setproperty_by_path(parent_id, "DescCtn", "Visible", "true");

      local pedit = ui_utility.get_ctrl_ptr_by_path(parent_id, "DescCtn\\DescEdt");
      if pedit ~= nil then
        pedit:SetFocus();
        pedit:SelectAll();
      end;

      local uin = tostring(sub_account_view.get_uin_by_id(id));
      sub_account_view.report_account_operation(sub_account_view.OPT_EDIT, uin);
    end;

    -- 响应小号昵称编辑框失去焦点消息
    on_desc_edt_kill_focus = function(uilib, id)
      local desc_ctn_id = ui_utility.get_ctrl_parent_id(id);
      local account_ctn_id = ui_utility.get_ctrl_parent_id(desc_ctn_id);
      ui_utility.setproperty_by_id(desc_ctn_id, "Visible", "false");

      local pedit = ui_utility.get_ctrl_ptr_by_path(desc_ctn_id, "DescEdt");
      if pedit == nil then
        return;
      end;

      local cui_nickname = pedit:GetText();
      local u8_nickname = CUIStrtoU8(cui_nickname);
      if string.len(u8_nickname) == 0 then
        return;
      end;

      local ma_mgr =
          base_utility.get_component_adapter("IMutiAccountMgr",
              "ierd_tgp.muti_account_mgr.GetMutiAccountMgrModule();");
      local info = ierd_tgp.muti_account_mgr.AccountInfo:new_local();
      info.uin = tostring(sub_account_view.get_uin_by_id(id));
      info.nickname = u8_nickname;
      ma_mgr:set_account_info(sub_account_view.game_id, info);

      ui_utility.setproperty_by_path(account_ctn_id, "DescStc", "Caption", u8_nickname);
    end;

    -- 响应编辑按钮被点击消息
    on_edit_btn_clk = function(uilib, id)
      local op_ctn_id = ui_utility.get_ctrl_parent_id(id);
      local account_ctn_id = ui_utility.get_ctrl_parent_id(op_ctn_id);
      local nick_name_stc_id = ui_utility.get_ctrl_id_by_path(account_ctn_id, "DescStc");
      sub_account_view.on_desc_edt_lbtn_up(uilib, nick_name_stc_id);
    end;

    -- 响应显示/隐藏小号列表对话框消息
    on_switch_account_list_dlg = function(json_str)
      local tbl = base_utility.json_to_table(json_str);
      if tbl == nil or tbl.data == nil or tbl.data.game_id == nil then
        return;
      end;

      if tbl.data.game_id == 0 then
        sub_account_view.do_close();
        return;
      end;

      local pdlg = ui_utility.get_ctrl_ptr_by_cid(CID_SUB_ACCOUNT_LIST_DIG);
      if pdlg ~= nil and pdlg:IsVisible() == TRUE then
        sub_account_view.do_close();
      else
        sub_account_view.show(tbl.data.game_id);
      end;
    end;

    -- 响应banner项选中态变化消息
    on_banner_focus_change = function(json_str)
      local tbl = base_utility.json_to_table(json_str);
      if tbl ~= nil and tbl.module_id ~= nil and 
        tostring(tbl.module_id) == tostring(sub_account_view.game_id) then
        return;
      end;

      sub_account_view.do_close();
    end;

    -- 响应主界面normal/mini模式变化消息
    on_mainframe_mode_change = function(json_str)
      sub_account_view.do_close();
    end;

    -- 响应昵称输入框键盘按下消息
    on_edit_key_down = function(uilib, id, dwVirtKey, dwKeyData)
      local VK_RETURN = 13;
      if dwVirtKey == VK_RETURN then
        sub_account_view.on_desc_edt_kill_focus(uilib, id);
      end;
    end;

    -- 关闭窗口
    do_close = function()
      local dlg_id = ui_utility.get_ctrl_id_by_cid(CID_SUB_ACCOUNT_LIST_DIG);
      layout_mgr.delay_remove_ctrl(dlg_id);
    end;

    -- 刷新小号列表
    refresh_sub_account_list = function()
      local dlg_id = ui_utility.get_ctrl_id_by_cid(CID_SUB_ACCOUNT_LIST_DIG);
      if dlg_id == Tenio.INVALID_CONTROLID then
        return;
      end;
      local pdlg = ui_utility.get_ctrl_ptr_by_id(dlg_id);
      local pAddCtn = ui_utility.get_ctrl_ptr_by_path(dlg_id, "AddAccountCtn");
      local pAccountCtn1 = ui_utility.get_ctrl_ptr_by_path(dlg_id, "AccountCtn1");
      local pAccountCtn2 = ui_utility.get_ctrl_ptr_by_path(dlg_id, "AccountCtn2");
      local pAccountCtn3 = ui_utility.get_ctrl_ptr_by_path(dlg_id, "AccountCtn3");
      if pdlg == nil or pAddCtn == nil or 
          pAccountCtn1 == nil or pAccountCtn2 == nil or pAccountCtn3 == nil then
        return;
      end;

      sub_account_view.id_to_uin = {};

      local ma_mgr =
          base_utility.get_component_adapter("IMutiAccountMgr",
              "ierd_tgp.muti_account_mgr.GetMutiAccountMgrModule();");
      local subacclist = ierd_tgp.muti_account_mgr.AccountList:new_local();
      ma_mgr:get_account_list(sub_account_view.game_id, subacclist);

      local dlg_rc = pdlg:GetRectEx(FALSE,TRUE);
      local dlg_width = dlg_rc.right - dlg_rc.left;
      local add_ctn_rc = pAddCtn:GetRectEx(FALSE,TRUE);
      local add_ctn_width = add_ctn_rc.right - add_ctn_rc.left;
      local account_ctn_rc = pAccountCtn1:GetRectEx(FALSE,TRUE);
      local account_ctn_width = account_ctn_rc.right - account_ctn_rc.left;
      local interval_width = 5;

      if subacclist.cnt == 0 or subacclist.cnt < 0 then
        pAccountCtn1:Show(FALSE);
        pAccountCtn2:Show(FALSE);
        pAccountCtn3:Show(FALSE);
        add_ctn_rc.left = (dlg_width - add_ctn_width) / 2;
        add_ctn_rc.right = add_ctn_rc.left + add_ctn_width;
        pAddCtn:MoveEx(add_ctn_rc,TRUE);
      elseif subacclist.cnt == 1 then
        pAccountCtn1:Show(TRUE);
        pAccountCtn2:Show(FALSE);
        pAccountCtn3:Show(FALSE);
        add_ctn_rc.left = (dlg_width - add_ctn_width - account_ctn_width - interval_width) / 2;
        add_ctn_rc.right = add_ctn_rc.left + add_ctn_width;
        pAddCtn:MoveEx(add_ctn_rc,TRUE);
        account_ctn_rc.left = add_ctn_rc.right + interval_width;
        account_ctn_rc.right = account_ctn_rc.left + account_ctn_width;
        pAccountCtn1:MoveEx(account_ctn_rc,TRUE);
        sub_account_view.set_account_info(
          pAccountCtn1, subacclist.infos[0].uin, subacclist.infos[0].nickname);
      elseif subacclist.cnt == 2 then
        pAccountCtn1:Show(TRUE);
        pAccountCtn2:Show(TRUE);
        pAccountCtn3:Show(FALSE);
        add_ctn_rc.left = 
          (dlg_width - add_ctn_width - account_ctn_width * 2 - interval_width * 2) / 2;
        add_ctn_rc.right = add_ctn_rc.left + add_ctn_width;
        pAddCtn:MoveEx(add_ctn_rc,TRUE);
        account_ctn_rc.left = add_ctn_rc.right + interval_width;
        account_ctn_rc.right = account_ctn_rc.left + account_ctn_width;
        pAccountCtn1:MoveEx(account_ctn_rc,TRUE);
        account_ctn_rc.left = account_ctn_rc.right + interval_width;
        account_ctn_rc.right = account_ctn_rc.left + account_ctn_width;
        pAccountCtn2:MoveEx(account_ctn_rc,TRUE);
        sub_account_view.set_account_info(
          pAccountCtn1, subacclist.infos[0].uin, subacclist.infos[0].nickname);
        sub_account_view.set_account_info(
          pAccountCtn2, subacclist.infos[1].uin, subacclist.infos[1].nickname);
      elseif subacclist.cnt > 2 then
        pAccountCtn1:Show(TRUE);
        pAccountCtn2:Show(TRUE);
        pAccountCtn3:Show(TRUE);
        add_ctn_rc.left = 
          (dlg_width - add_ctn_width - account_ctn_width * 3 - interval_width * 3) / 2;
        add_ctn_rc.right = add_ctn_rc.left + add_ctn_width;
        pAddCtn:MoveEx(add_ctn_rc,TRUE);
        account_ctn_rc.left = add_ctn_rc.right + interval_width;
        account_ctn_rc.right = account_ctn_rc.left + account_ctn_width;
        pAccountCtn1:MoveEx(account_ctn_rc,TRUE);
        account_ctn_rc.left = account_ctn_rc.right + interval_width;
        account_ctn_rc.right = account_ctn_rc.left + account_ctn_width;
        pAccountCtn2:MoveEx(account_ctn_rc,TRUE);
        account_ctn_rc.left = account_ctn_rc.right + interval_width;
        account_ctn_rc.right = account_ctn_rc.left + account_ctn_width;
        pAccountCtn3:MoveEx(account_ctn_rc,TRUE);
        sub_account_view.set_account_info(
          pAccountCtn1, subacclist.infos[0].uin, subacclist.infos[0].nickname);
        sub_account_view.set_account_info(
          pAccountCtn2, subacclist.infos[1].uin, subacclist.infos[1].nickname);
        sub_account_view.set_account_info(
          pAccountCtn3, subacclist.infos[2].uin, subacclist.infos[2].nickname);
      end;
    end;

    set_account_info = function(item_ctn_ptr, uin, nick_name)
      if item_ctn_ptr == nil then
        return;
      end;
      local item_ctn_id = item_ctn_ptr:GetControlID();

      local icon = "ONEIMAGE:login\\icon\\icon_QQ.png|http://q3.qlogo.cn/g?b=qq&nk=" .. 
          uin .. "&s=40&t=1321416654;Stretch;0;";
      ui_utility.setproperty_by_path(item_ctn_id, "DescStc", "Caption", nick_name);
      ui_utility.setproperty_by_path(item_ctn_id, "DescCtn\\DescEdt", "Caption", nick_name);
      ui_utility.setproperty_by_path(item_ctn_id, "ImgCtn", "Background", icon);
      sub_account_view.set_id_to_uin(item_ctn_id, "DescStc", uin);
      sub_account_view.set_id_to_uin(item_ctn_id, "OpCtn\\LaunchBtn", uin);
      sub_account_view.set_id_to_uin(item_ctn_id, "OpCtn\\EditBtn", uin);
      sub_account_view.set_id_to_uin(item_ctn_id, "OpCtn\\DelBtn", uin);
      sub_account_view.set_id_to_uin(item_ctn_id, "DescCtn\\DescEdt", uin);
    end;

    set_id_to_uin = function(item_ctn_id, path, uin)
      local id = ui_utility.get_ctrl_id_by_path(item_ctn_id, path);
      sub_account_view.id_to_uin[id] = uin;
    end;

    get_uin_by_id = function(id)
      return sub_account_view.id_to_uin[id];
    end;

    report_account_operation = function(op_type, be_op_uin)
        local ma_mgr =
            base_utility.get_component_adapter("IMutiAccountMgr",
                "ierd_tgp.muti_account_mgr.GetMutiAccountMgrModule();");
        local subacclist = ierd_tgp.muti_account_mgr.AccountList:new_local();
        ma_mgr:get_account_list(sub_account_view.game_id, subacclist);

        local qosparam = ierd_tgp.tpf_ui.LuaQosData:new();
        qosparam.oc_type = ierd_tgp.tpf_ui.kInstant;
        qosparam.qos_report_id = ierd_tgp.adapt_for_imports.qos.Qos_kind.kWebGameHelperMoreClient;
        qosparam.int_val[0] = op_type;
        qosparam.int_val[1] = sub_account_view.game_id;
        qosparam.int_val[2] = subacclist.cnt;
        qosparam.int_count = 3;
        qosparam.str_val1 = tostring(be_op_uin);
        qosparam.str_val2 = tostring(sub_account_view.game_id)
        qosparam.str_count = 2;
        base_utility.lua_qos(qosparam);
        qosparam:delete();
    end;
};

sub_account_view.init();